//
//  ViewController.swift
//  ThenDemo
//
//  Created by 李煜 on 2024/5/13.
//

import UIKit
import Then


class ViewController: UIViewController {

	override func viewDidLoad() {
		super.viewDidLoad()
    //then - 引用类型对象
    //with - 值类型对象
    //do - 执行方法,例如按钮执行selector
		let label = UILabel().then {
			$0.textAlignment = .center
			$0.textColor = .black
			$0.text  = "hello , world"
			$0.frame = .init(x: 20, y: 50, width: 100, height: 44)
			$0.backgroundColor = .brown
		}
		
		view.addSubview(label)
    
    let lb = UILabel()
    view.addSubview(lb)
    lb.backgroundColor = .orange
    lb.frame = CGRectMake(20, 100, 100, 30)
    
    //忽略Result of call to 'then' is unused警告 最好在lb创建时即使用then with ,虽然不影响结果
    _ = lb.then { z in
      z.backgroundColor = .red
    }
	}

	func test1() {
			//自定义对象
		let instance: XMPersion = XMPersion().then { p in
			p.name = "大白"
		}
		
			//with复制值类型
		let oldFrame: CGRect = .zero
		let newFrame = oldFrame.with {
			$0.size.width = 200
			$0.size.height = 100
		}
		newFrame.width//200
		newFrame.height//100
		
		UserDefaults.standard.do {
			$0.set("大白", forKey: "name")
			$0.set("xxx@gmail.com", forKey: "email")
			
		}
	}
}
class XMPersion: Then {
	var name: String = ""
}

/**
 The `Then` library is a lightweight Swift library created by `devxoul` that provides syntactic sugar for creating and configuring objects. It allows you to initialize and configure objects in a clean and concise way using closures. It is especially useful when you need to configure UIKit elements or other objects right after initialization.
 
 ### Installation
 
 You can add `Then` to your project using Swift Package Manager, CocoaPods, or Carthage.
 
 #### Using Swift Package Manager:
 
 1. Open your project in Xcode.
 2. Go to `File` > `Add Packages...`
 3. Paste the URL: `https://github.com/devxoul/Then`
 4. Choose the version and add the package.
 
 #### Using CocoaPods:
 ```ruby
 pod 'Then'
 ```
 
 #### Using Carthage:
 ```ruby
 github "devxoul/Then"
 ```
 
 ### Basic Usage
 
 The core of `Then` is the `then` method, which allows you to initialize and configure an object in a single block.
 
 Here’s an example of how you would typically use `Then`:
 
 ```swift
 import Then
 
 let label = UILabel().then {
 $0.text = "Hello, World!"
 $0.textColor = .blue
 $0.textAlignment = .center
 $0.numberOfLines = 0
 }
 ```
 
 With the `then` block, you can create a `UILabel`, set its properties, and configure it all in one step, making the code more readable and reducing the amount of boilerplate code.
 
 ### Usage Variants
 
 There are a few ways to use `Then` depending on the context and the specific object you are working with.
 
 1. **Using `then` for Initializing and Configuring Objects**
 
 The `then` method allows you to configure an object right after creating it. You can use it with any class that inherits from `NSObject`.
 
 ```swift
 let button = UIButton(type: .system).then {
 $0.setTitle("Tap Me", for: .normal)
 $0.setTitleColor(.white, for: .normal)
 $0.backgroundColor = .blue
 }
 ```
 
 2. **Using `with` for Value Types**
 
 The `with` method is useful for **value types** (like structs and enums). This method creates a copy of the value type, applies the changes, and returns it.
 
 ```swift
 let frame = CGRect(x: 0, y: 0, width: 100, height: 50).with {
 $0.origin.x = 10
 $0.size.height = 100
 }
 ```
 
 In this case, `with` is a more appropriate method for `CGRect` (a struct) because it handles value copying.
 
 3. **Using `do` for Executing Functions on Objects**
 
 The `do` method can be used to perform actions on an object. This is useful if you want to call methods or perform other operations that don’t involve directly setting properties.
 
 ```swift
 let data = Data().do {
 print($0.count)
 }
 ```
 
 Here, `do` is used to execute a block of code related to the `data` object.
 
 ### How They Differ:
 
 1. **`then`**:
 - Use it with classes or reference types.
 - Good for initializing and configuring the object immediately after creation.
 - Example:
 ```swift
 let view = UIView().then {
 $0.backgroundColor = .red
 $0.layer.cornerRadius = 8
 }
 ```
 
 2. **`with`**:
 - Use it with value types like structs or enums.
 - It creates a copy, applies modifications, and returns the modified copy.
 - Example:
 ```swift
 let frame = CGRect(x: 0, y: 0, width: 100, height: 50).with {
 $0.size.width = 200
 }
 ```
 
 3. **`do`**:
 - Use it to perform actions that do not change properties (e.g., calling methods or printing).
 - Best used when you need to perform actions on an object without modifying it.
 - Example:
 ```swift
 let button = UIButton().do {
 $0.setTitle("Button", for: .normal)
 $0.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
 }
 ```
 
 ### When to Use Each
 
 - **`then`** is ideal for most configurations of UI elements or objects where you want to modify properties.
 - **`with`** is best when working with structs or enums where you want to copy the value, make modifications, and return the modified version.
 - **`do`** is useful for executing code blocks that don't involve directly changing properties, such as calling methods or performing side effects.
 
 ### Example with UIKit Components
 
 ```swift
 import UIKit
 import Then
 
 class MyViewController: UIViewController {
 
 let label = UILabel().then {
 $0.text = "Hello, Swift!"
 $0.textColor = .white
 $0.backgroundColor = .black
 $0.textAlignment = .center
 $0.font = UIFont.systemFont(ofSize: 24)
 }
 
 override func viewDidLoad() {
 super.viewDidLoad()
 view.addSubview(label)
 label.frame = CGRect(x: 50, y: 100, width: 200, height: 50)
 }
 }
 ```
 
 In this example:
 - `then` is used to initialize and configure the `UILabel`.
 - The code is more concise and easier to read, as all the properties of `label` are set in one block.
 
 ### Summary
 
 - **`then`** is used for classes or reference types to configure objects.
 - **`with`** is used for value types to return modified copies.
 - **`do`** is used to execute methods or perform actions without directly changing the object's properties.
 
 Using `Then` helps you write cleaner and more expressive code, especially when configuring UI components in iOS development.
 
 
 */
